﻿using JustTest.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.ModelBinding;
using Microsoft.AspNet.Identity;

namespace JustTest
{
    public partial class Profile : System.Web.UI.Page
    {
        private static JustTestDb context = new JustTestDb();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        // The id parameter should match the DataKeyNames value set on the control
        // or be decorated with a value provider attribute, e.g. [QueryString]int id
        public JustTest.Models.ApplicationUser FormViewProfile_GetItem([QueryString]string username)
        {
            if (username == null)
            {
                if (User.Identity.IsAuthenticated)
                {
                    username = User.Identity.GetUserName();
                }
                else
                {
                    this.Response.Redirect("ErrorPages/NotFoundPage.aspx");
                }
            }

            JustTest.Models.ApplicationUser user = context.Users.FirstOrDefault(u => u.UserName == username);

            if (user == null)
            {
                if (User.Identity.IsAuthenticated)
                {
                    username = User.Identity.GetUserName();
                    user = context.Users.FirstOrDefault(u => u.UserName == username);
                }
                else
                {
                    this.Response.Redirect("~/ErrorPages/NotFoundPage.aspx");
                }
            }

            return user;
        }

        protected void GridViewTests_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridViewTests.PageIndex = e.NewPageIndex;
        }

        protected void GridViewScores_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridViewScores.PageIndex = e.NewPageIndex;
        }
       
        public IQueryable<JustTest.Models.Test> GridViewTests_GetData()
        {
            string userName = this.Request.Params["username"];
            var user = context.Users.FirstOrDefault(u => u.UserName == userName);

            if (user == null)
            {
                this.Response.Redirect("~/ErrorPages/NotFoundPage.aspx");
            }

            return context.Tests.Include("User").Where(test => 
                test.Status == TestStatus.Active &&
                test.User.UserName == user.UserName)
                .OrderBy(test => test.Id);
        }

        public IEnumerable<JustTest.Models.TestResult> RepeaterTestResults_GetData()
        {
            var logedUserName = Context.User.Identity.Name;

            return context.TestResults.Include("User").Where(testResult => testResult.User.UserName == logedUserName);
        }

        // The return type can be changed to IEnumerable, however to support
        // paging and sorting, the following parameters must be added:
        //     int maximumRows
        //     int startRowIndex
        //     out int totalRowCount
        //     string sortByExpression
        public IQueryable<JustTest.Models.TestResult> GridViewScores_GetData()
        {
            return context.TestResults.OrderBy(testResult => testResult.Id);
        }

       

    }
}